﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algorithm.Struct
{
    public class Queue<T>
    {
        private int _length = 0;
        private T[] _queue;

        private int _tail = 0;
        private int _head = 0;

        public Queue(int length)
        {
            if (length <= 0)
            {
                throw new ArgumentException();
            }
            _length = length;
            _queue = new T[length];
        }

        public bool IsEmpty()
        {
            return _tail == _head;
        }

        public bool IsFull()
        {
            return _tail + 1 == _head;
        }

        public void Enqueue(T t)
        {
            if (IsFull())
            {
                throw new Exception("Overflow");
            }
            _queue[_tail] = t;
            if (_tail + 1 == _length)
            {
                _tail = 0;
            }
            else
            {
                _tail++;
            }
        }

        public T Dequeue()
        {
            if (IsEmpty())
            {
                throw new Exception("underfolw");
            }
            T result = _queue[_head];
            if (_head + 1 == _length)
            {
                _head = 0;
            }
            else
            {
                _head++;
            }
            return result;
        }
    }
}
